﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class chitiet : System.Web.UI.Page
{
    GioHangDaTa gh;
    DataTable dt = new DataTable();
    KetNoi obj = new KetNoi();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadTatCaYKien();
            txtemail.Text = Convert.ToString(Session["id"].ToString());
            txtemail.Enabled = false;
            string ma = Request.QueryString["masp"];
            dt = obj.loadDataTable("SELECT * FROM DienThoai WHERE masp =" + ma);
            Repeater5.DataSource = dt;
            Repeater5.DataBind();
            LoadRepeaterNhanXet();
            LoadRepeaterYKienChung();
            LoadTatCaYKien();
        }
        
      
    }

    protected void LoadRepeaterNhanXet()
    {
        string ma = Request.QueryString["masp"];
        dt = obj.loadDataTable("SELECT * FROM DienThoai WHERE masp =" + ma);
        Repeater6.DataSource = dt;
        Repeater6.DataBind();
    }

    protected void LoadRepeaterYKienChung()
    {
        string ma = Request.QueryString["masp"];
        dt = obj.loadDataTable("SELECT * FROM DienThoai WHERE masp =" + ma);
        Repeater7.DataSource = dt;
        Repeater7.DataBind();
    }
    protected void LoadTatCaYKien()
    {
        string ma = Request.QueryString["masp"];
        dt = obj.loadDataTable("SELECT KhachHang.HoTen, KhachHang.Email, NhanXet.TieuDe, NhanXet.ThoiGian, NhanXet.NhanXet FROM KhachHang, NhanXet WHERE KhachHang.KhachHangID = NhanXet.KhachHangID  AND masp =" + ma + " ORDER BY ThoiGian DESC");
        Repeater8.DataSource = dt;
        Repeater8.DataBind();
    }

 protected void ImageButton5_Click(object sender, ImageClickEventArgs e)//NUT MUA: THEM SANPHAM VAO GIO HANG
    {

       gh = new GioHangDaTa();
        DataTable tb = new DataTable();
       if (Session["GioHang"] == null)
           tb=gh.setTable();
        
        else
            tb = (DataTable)Session["GioHang"];// Tao ra 1 gio hang voi cai truong rong
        string query = "SELECT * FROM DienThoai WHERE masp =" + Request.QueryString["masp"];// Lay thongtin sp wa masp dc chon o trang chi tiet
        
        Data dt = new Data();
        OleDbDataReader dr= dt.getData(query);
       
        if (dr.Read())
        {
            tb = gh.dienVaoBang(tb, dr[9].ToString(), dr["tensp"].ToString(), 1, Convert.ToDouble(dr["gia"]), dr["masp"].ToString());
            Session["GioHang"] = tb; // Gan bien Session["GioHang"] = sp da chon
        }
        Response.Redirect("GioHang.aspx", true);      
    }
    protected void XoaText()
    {
        txttieude.Text = "";
        txtnoidung.Text = "";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (Session["id"].ToString() == "")
        {
            Response.Write("<script language='javascript'> alert('Bạn chưa đăng nhập' ) </script>");
            Response.Redirect("DangNhap.aspx");

        }
        else
        {
            string tieude = txttieude.Text.ToString();
            string noidung = txtnoidung.Text.ToString();
            string thoigian = DateTime.Now.ToString();
            string masp = Request.QueryString["masp"];
            string email = Session["id"].ToString();
            dt = obj.loadDataTable("Select * From KhachHang where KhachHang.Email='" + email + "'");
            string makhachhang = dt.Rows[0][0].ToString();// Lay makhach hang qua email
            obj.ThemNhanXet(tieude, noidung, thoigian, makhachhang, masp);
            XoaText();
            Response.Write("<script language='javascript'> alert('Ý kiến của bạn đã được gửi. Cảm ơn bạn !' ) </script>");
            LoadTatCaYKien();
        }
        
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("DangNhap.aspx");
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {

    }
    protected void Repeater5_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
}
